package borge.web.teamspirit.client.panels;

import java.io.Serializable;
import java.util.List;

import borge.web.teamspirit.client.TeamSpirit;
import borge.web.teamspirit.client.data.Match;
import borge.web.teamspirit.client.data.Person;
import borge.web.teamspirit.client.data.Serie;
import borge.web.teamspirit.client.data.Team;
import borge.web.teamspirit.client.data.TeamPlayer;
import borge.web.teamspirit.client.interfaces.MenuEventHandler;
import borge.web.teamspirit.client.interfaces.PanelHandler;
import borge.web.teamspirit.client.interfaces.ServerCallbackHandler;
import borge.web.teamspirit.client.interfaces.WidgetEventHandler;
import borge.web.teamspirit.client.servercomm.DBQuery;
import borge.web.teamspirit.client.servercomm.DBQueryResult;
import borge.web.teamspirit.client.servercomm.ServerComm;
import borge.web.teamspirit.client.widgets.ListDataWidget;
import borge.web.teamspirit.client.widgets.ListMatchesWidget;
import borge.web.teamspirit.client.widgets.ListPersonsWidget;
import borge.web.teamspirit.client.widgets.ListPlayersWidget;
import borge.web.teamspirit.client.widgets.ListSeriesWidget;
import borge.web.teamspirit.client.widgets.ListTeamsWidget;

import com.google.gwt.user.client.Window;

public class ListPanel extends MiddlePanel implements WidgetEventHandler, MenuEventHandler, ServerCallbackHandler {
	
	private ListPersonsWidget iPersonsWidget;
	private ListPlayersWidget iPlayersWidget;
	private ListSeriesWidget iSeriesWidget;
	private ListMatchesWidget iMatchesWidget;
	private ListTeamsWidget iTeamsWidget;

	@SuppressWarnings("unchecked")
	private ListDataWidget iActiveWidget;
	
	public ListPanel(/*ServerComm aServerComm,*/ PanelHandler aHandler){
			
		super(/*aServerComm,*/ aHandler);
		//iCreatePlayerWidget = new CreatePlayerWidget(this, null);
		//iSerieWidget = new ShowSerieWidget(null, this);
		
		//add(iCreatePlayerWidget);
		
	}
	
	public void handleWidgetEvent(WidgetEventType aEvent){
		
		switch(aEvent){

		case DELETE_PLAYER: {
			
			TeamPlayer p = iPlayersWidget.getDataObject();
			ServerComm.deletePlayer(p, this);
			break;
		}
		case DELETE_SERIE: {
		    Serie s = iSeriesWidget.getDataObject();
		    ServerComm.deleteSerie(s, this);
		    break;
		}
		
		case DELETE_MATCH: {
		    Match m = iMatchesWidget.getDataObject();
		    ServerComm.deleteMatch(m, this);
		    break;
		}
		case EDIT_PLAYER:
		case EDIT_SERIE:
		case EDIT_MATCH: {
		    iPanelHandler.switchPanel(aEvent, iActiveWidget);
		    break;
		}
		case SHOW_PLAYER:
		case SHOW_SERIE:
		case SHOW_MATCH:{
		    iPanelHandler.switchPanel(aEvent, iActiveWidget);
		    break;
		}
		}
		
	}
	
	
	public boolean handleMenuEvent(String aLinkText){
	    
	    boolean handled = false;
	    if(aLinkText.equals(TeamSpirit.getConstants().list_teams())){
	        //getAllData(Team.class.getName());
	    	ServerComm.getObjectList(new DBQuery<Team>(Team.class.getName()), this);
	        handled = true;
	    }
	    else if(aLinkText.equals(TeamSpirit.getConstants().list_users())){
	        //getAllData(Person.class.getName());
	
	    	ServerComm.getObjectList(new DBQuery<Person>(Person.class.getName()), this);
	        handled = true;
	    }
	    else if(aLinkText.equals(TeamSpirit.getConstants().list_players())){
	    	ServerComm.getObjectList(new DBQuery<TeamPlayer>(TeamPlayer.class.getName()), this);
	        //getAllData(TeamPlayer.class.getName());
	        handled = true;
	    }
	    else if(aLinkText.equals(TeamSpirit.getConstants().list_series())){
	    	//ServerComm.getObjectList(new DBQuery<Person>(.class.getName()), this);
	        //getAllData(Serie.class.getName());
	        handled = true;
	    }
	    else if(aLinkText.equals(TeamSpirit.getConstants().list_matches())){
	    	ServerComm.getObjectList(new DBQuery<Match>(Match.class.getName()), this);
	        //getAllData(Match.class.getName());
	        handled = true;
	    }
	    
	    return handled;
	    
	}
	
	private void getAllData(String aClassName){
        DBQuery query = new DBQuery(aClassName);
        
        
        //query.setFilter(aId + " > IdParam");
        //query.setOrdering("iMatchId desc");
        //query.setParameters("Long IdParam");
        //query.setValueType(DBQuery.LONG_TYPE);
        //query.setLongValue(new Long(4));
        ServerComm.getObjectList(query, this);
	}
	
	
	
    @SuppressWarnings("unchecked")
    public <T extends Serializable> void handleServerCallback(DBQueryResult<T> aResult, CallbackType aType){
        
        //remove(0);
    	//Window.alert("callback");
    	if(aResult.queryFailed()){
    		Window.alert("failed:" + aResult.getErrorMsg());
    		return;
    	}
    	
    	else{ 
        
			switch (aType) {
			case RETRIEVED_DATA: {

				if (aResult.queryFailed())
					Window.alert("failed");

				if (aResult.getResultList() == null)
					Window.alert("list is null");

				if(aResult.getResultList().size() == 0){
				    Window.alert("list is empty");
				    return;
				}
				// This is probably very wrong...
				List<T> list = aResult.getResultList();
				T t = list.get(0);

				if(t instanceof Team){
					
					iTeamsWidget = new ListTeamsWidget(this, "test", (List<Team>)aResult.getResultList());
					if (iActiveWidget != null)
						remove(iActiveWidget);

					add(iTeamsWidget);
					iActiveWidget = iTeamsWidget;
				}
				
				else if(t instanceof Person){
					//Window.alert("is a perso");
                    iPersonsWidget = new ListPersonsWidget(this,
                            (List<Person>) aResult.getResultList());

                    if (iActiveWidget != null)
                        remove(iActiveWidget);

                    add(iPersonsWidget);
                    iActiveWidget = iPersonsWidget;
				}

				
				else if(t instanceof TeamPlayer){
                    iPlayersWidget = new ListPlayersWidget(this,
                            (List<TeamPlayer>) aResult.getResultList());

                    if (iActiveWidget != null)
                        remove(iActiveWidget);

                    add(iPlayersWidget);
                    iActiveWidget = iPlayersWidget;
				}
				else if(t instanceof Serie){
                    iSeriesWidget = new ListSeriesWidget(this, (List<Serie>)aResult.getResultList());

                    if (iActiveWidget != null)
                        remove(iActiveWidget);

                    add(iSeriesWidget);
                    iActiveWidget = iSeriesWidget;
				}
				else if(t instanceof Match){

                    iMatchesWidget = new ListMatchesWidget(this, (List<Match>)aResult.getResultList());
                    
                    if (iActiveWidget != null)
                        remove(iActiveWidget);

                    add(iMatchesWidget);
                    iActiveWidget = iMatchesWidget;
				}
				break;
			}
			case DELETED: {

				iActiveWidget.objectDeleted();
				break;

			}
			}
    	}

    }
    
    public void handleServerError(Throwable t){
        Window.alert("Server error: " + t.toString());
    }

}

